草庐IT

c++ - Clang、std::next、libstdc++ 和 constexpr-ness

全部标签

c++ - std::bad_alloc 不进入交换空间

我试图理解为什么当我似乎有足够的(虚拟?)可用内存时我会收到std::bad_alloc异常。本质上,我有一个素数生成器(Eratosthenes筛法(尚未分段)),我在其中为指示器数组更新bool值,然后为我在命令行指定的范围内找到的素数更新整数。我有1GB内存(其中一些会被我的操作系统(ubuntu10.04)占用,并且可能其中一些不可用作堆内存(我在这里错了吗?))和2.8GB交换空间(我相信这是在安装Ubuntu时为我自动设置的)如果我将上限设置为600000000,那么我需要0.6GB的内存用于我的指标数组和大约30000000*4字节(略微高估,因为有26355867个小于

c++ - 'mapped_type' std::map 错误的初始化没有匹配的构造函数

我有一个名为“Card”的类,我试图将它的一些对象存储在std::map中卡片.hpp:classCard{public:enumValueType{NOVALUE,ACE};enumFaceType{NOFACE,CLUBS};Card(constValueType&_value,constFaceType&_face);Card(constCard&_card);private:ValueTypem_value;FaceTypem_face;};以下是我存储和访问它的方式:甲板.hpp:#includeclassCard;classDeck{public:Deck();std::s

c++ - 是否可以推断出 std::insert_iterator 包含的类型?

我有一个需要模板化迭代器类型的函数。它当前取消引用迭代器以检查被迭代的类型。templatevoidfunc(Iteratori){//Inspectthesizeoftheobjectsbeingiteratedconstsize_ttype_size=sizeof(*i);...}我最近发现一些标准迭代器类型,例如std::insert_iterator将*i定义为对i的简单引用.即sizeof(*i)是迭代器本身的大小;与sizeof(i)或sizeof(***i)相同是否有一种通用方法(支持C++03)来确定任何标准迭代器正在迭代的对象的大小或类型?

c++ - 在这种情况下,为什么 g++ 和 clang 会破坏 namespace 抽象?

编译:structstr{};namespacea{voidfoo(strs){}}namespaceb{voidfoo(strs){}voidbar(strs){foo(s);}}intmain(int,char**){return0;}但这不是(将结构定义移到命名空间a内)namespacea{structstr{};voidfoo(strs){}}namespaceb{voidfoo(a::strs){}voidbar(a::strs){foo(s);}}intmain(int,char**){return0;}我得到的错误是bad.cpp:Infunction‘voidb::b

c++ - std::function 的异常安全

我尝试查找这段代码是否会抛出异常,但没有成功:std::functionf=[]{};根据标准,std::function的复制或移动构造函数不是noexcept。但我想缺少noexcept关键字是因为std::function还包装了用户定义的仿函数对象,其复制或移动构造函数可能会抛出。在我的例子中,异常(exception)似乎不太可能,但有可能吗? 最佳答案 Inmycaseanexceptionseemsveryunlikelybutisitpossibleatall?原则上是的。std::function将不得不分配内存来

c++ - 如何将 tr1/array 转换为 std::string?

我想知道如何投std::tr1::array到std::string?编译器总是报错,我试过了std::tr1::arraysss;string(sss);stringasd(sss);两者都有效... 最佳答案 unsignedchar让这变得棘手。如果您知道您的系统使用2s补码1字节8位unsignedchar和char,以及从unsignedchar到char的隐式转换做你想做的(这些并不总是正确的!),并且你的数组缓冲区是null终止的(即,第一个0之后的字符应该被丢弃),这个函数有效:templatestd::string

c++ - 从 c++11 中的用户定义文字返回 std::array

我刚刚安装了gcc-4.8.1,当我意识到我可以执行-std=c++1y并获得多行constexpr时,我非常兴奋。我很想知道,是否有办法使这项工作正常进行?#includeconstexprautooperator""_a1(constchar*text,constsize_tsize)->std::array{std::array()blah;std::strncpy(blah.data(),test,size);//dosomestufftoblahatcompiletimereturnblah;}intmain(){autoblah="helloworld"_a2;}但是我变得

c++ - x86_64 和 ARM 上的原子 CAS 操作是否始终使用 std::memory_order_seq_cst?

作为AnthonyWilliamssaid:some_atomic.load(std::memory_order_acquire)doesjustdropthroughtoasimpleloadinstruction,andsome_atomic.store(std::memory_order_release)dropsthroughtoasimplestoreinstruction.众所周知,在x86上,操作load()和store()内存屏障memory_order_consume,memory_order_acquire,memory_order_release,memory_o

c++ - 带有 constexpr 的编译时命名参数习语

我最近遇到了很多情况,其中命名参数习语很有用,但我希望它在编译时得到保证。在链中返回引用的标准方法似乎总是调用运行时构造函数(使用Clang3.3-O3编译)。我无法找到与此相关的任何内容,所以我试图让它与constexpr一起工作并获得一些功能:classFoo{private:int_a;int_b;public:constexprFoo():_a(0),_b(0){}constexprFoo(inta,intb):_a(a),_b(b){}constexprFoo(constFoo&other):_a(other._a),_b(other._b){}constexprFooSet

c++ - 使用带有静态 constexpr 的模板类时如何修复链接错误?

我有以下代码#includetemplateclassA{public:staticconstexprintarr[5]={1,2,3,4,5};};templateconstexprintA::arr[5];intmain(){Aa;std::cout编译顺利,但我有一个我不明白的链接错误g++-std=c++11test.cpp-otest/tmp/ccFL19bt.o:Infunction`main':test01.cpp:(.text+0xa):undefinedreferenceto`A::arr'collect2:error:ldreturned1exitstatus